package com.gigmissions.server.servlets;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gigmissions.server.dao.GigModel;
import com.gigmissions.server.dao.PMF;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;


import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;


public class AddGigServlet extends HttpServlet {
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(AddGigServlet.class.getName());
	
		
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	
    	String venuename = req.getParameter("venuename");
    	String gigid = req.getParameter("gigid");
    	
    	//only the user who created the gig can set the details - if it's another user
    	// send back a response offering them to request a spot on the line-up
    	UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        
        
        
        
        
    	//get the gig at this venue
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	String query = "select from " + GigModel.class.getName() + " where id=\'" + gigid + "\'";
        List<GigModel> gigs = (List<GigModel>) pm.newQuery(query).execute();
    	
        //should never have more tha one
    	
        if(gigs==null||gigs.size()==0){
        	//couldnt find the requsted gig 
        }
        
        	
        
    	GigModel gig = gigs.get(0);    	
        
    	
        String address = req.getParameter("address");
        String lat = req.getParameter("lat");
        String lng = req.getParameter("lng");
        
   
        String title = req.getParameter("title");
        String description = req.getParameter("description");
        Date date = new Date();
        //Gig gig = new Gig(title,user, description, date,address,lat,lng);

      
        try {
            pm.makePersistent(gig);
        } finally {
            pm.close();
        }

       
    }
    
   
    
}
